package com.example.traning.mall.framework.jackson.core;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.experimental.UtilityClass;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;


/**
 * @author lei yu
 * @since 2024/06/15
 */
@UtilityClass
public class JacksonUtil {

    private static final DateTimeFormatter NORM_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter NORM_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter NORM_TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static void config(ObjectMapper objectMapper) {
        if (Objects.isNull(objectMapper)) {
            return;
        }
        // 1. 创建 SimpleModule 对象
        SimpleModule simpleModule = new SimpleModule();
        simpleModule
                // LocalDate
                .addSerializer(LocalDate.class, new LocalDateSerializer(NORM_DATE_FORMATTER))
                .addDeserializer(LocalDate.class, new LocalDateDeserializer(NORM_DATE_FORMATTER))
                // LocalTime
                .addSerializer(LocalTime.class, new LocalTimeSerializer(NORM_TIME_FORMATTER))
                .addDeserializer(LocalTime.class, new LocalTimeDeserializer(NORM_TIME_FORMATTER))
                // LocalDateTime
                .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(NORM_DATETIME_FORMATTER))
                .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(NORM_DATETIME_FORMATTER));
        // 2. 配置 objectMapper
        // 2.1 将 SimpleModule 注册到 objectMapper
        objectMapper.registerModules(simpleModule)
                // 2.2 忽略 null 属性
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                // 2.3 可序列化 null 对象
                .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                // 2.4 枚举转字符串
                .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true)
                // 2.5 忽略未知属性
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                // 2.6 忽略未知枚举
                .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);

    }
}
